package com.aisi.backend.controller;

import cn.hutool.core.util.StrUtil;
import com.aisi.backend.domain.RestBean;
import com.aisi.backend.domain.RestCode;
import com.aisi.backend.domain.entity.Account;
import com.aisi.backend.domain.entity.AccountDetails;
import com.aisi.backend.domain.vo.request.DetailsAccountSaveVo;
import com.aisi.backend.domain.vo.request.LoginAccountVo;
import com.aisi.backend.domain.vo.request.RegisterAccountVo;
import com.aisi.backend.domain.vo.request.ResetAccountVo;
import com.aisi.backend.domain.vo.response.AccountDetailsVo;
import com.aisi.backend.domain.vo.response.AccountVo;
import com.aisi.backend.domain.vo.response.LoginResponseVo;
import com.aisi.backend.service.AccountDetailsService;
import com.aisi.backend.service.AccountService;
import com.aisi.backend.service.impl.AccountDetailsServiceImpl;
import com.aisi.backend.service.impl.AccountServiceImpl;
import com.aisi.backend.utils.Const;
import com.aisi.backend.utils.JwtUtils;
import com.aisi.backend.utils.WebUtils;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * @Author: shenjianZ
 * @Date: 2024/5/1 上午11:27
 * @Description:
 */

@WebServlet("/api/user/*")
public class AccountController extends BaseController {

    private final AccountService accountService = new AccountServiceImpl();
    private final AccountDetailsService accountDetailsService = new AccountDetailsServiceImpl();

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RegisterAccountVo registerAccountVo = WebUtils.readJson(request, RegisterAccountVo.class);
        Integer i = accountService.register(registerAccountVo);
        if (i > 0) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.FAILURE);
            WebUtils.writeJson(response, restBean);
        }
    }


    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginAccountVo loginAccountVo = WebUtils.readJson(request, LoginAccountVo.class);
        LoginResponseVo vo = accountService.login(loginAccountVo);
        if (vo != null) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS, vo);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.USERNAME_OR_PASSWORD_ERROR);
            WebUtils.writeJson(response, restBean);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 获取token
        String token = request.getHeader("authorization");
        token = token.replace("Bearer ", "");
        if (StrUtil.isBlank(token)) {
            WebUtils.writeJson(response, RestBean.failure(RestCode.TOKEN_EMPTY));
            return;
        }
        String verifyToken = JwtUtils.resolveJwtToken(token);
        if (Const.TOKEN_INVALID.equals(verifyToken)) {
            WebUtils.writeJson(response, RestBean.failure(RestCode.TOKEN_INVALID));
        } else if (Const.TOKEN_EXPIRE.equals(verifyToken)) {
            WebUtils.writeJson(response, RestBean.failure(RestCode.TOKEN_EXPIRE));
        } else {
            WebUtils.writeJson(response, RestBean.success(RestCode.SUCCESS));
        }
    }

    public void resetPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResetAccountVo resetAccountVo = WebUtils.readJson(request, ResetAccountVo.class);
        Integer i = accountService.resetPassword(resetAccountVo);
        if (i > 0) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.USERNAME_OR_PASSWORD_ERROR);
            WebUtils.writeJson(response, restBean);
        }
    }

    public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = (Integer) request.getAttribute("userId");
        AccountVo vo = accountService.getById(userId);
        if (vo != null) {
            RestBean restBean = RestBean.success(vo);
            WebUtils.writeJson(response, restBean);
        }
    }

    public void details(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = (Integer) request.getAttribute("userId");
        AccountDetails accountDetails = accountDetailsService.findAccountDetailsById((userId));
        AccountDetailsVo vo = new AccountDetailsVo(accountDetails.getSex(), accountDetails.getPhone(), accountDetails.getQq(), accountDetails.getWx(), accountDetails.getDescription());
        if (vo != null) {
            RestBean restBean = RestBean.success(vo);
            WebUtils.writeJson(response, restBean);
        }
    }

    public void saveDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = (Integer) request.getAttribute("userId");
        DetailsAccountSaveVo vo = WebUtils.readJson(request, DetailsAccountSaveVo.class);
        boolean flag1 = accountDetailsService.saveAccountDetails(userId, vo);
        boolean flag2 = accountService.updateAccount(userId, vo.getEmail(), vo.getUsername());
        if (flag1 && flag2) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.FAILURE);
            WebUtils.writeJson(response, restBean);
        }
    }

}
